package com.dyson.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author:Dyson
 * @time:2019/10/21 18:34
 */
public class Receiver {
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.237.128");
        factory.setPort(5672);
        factory.setUsername("root");
        factory.setPassword("root");
        factory.setVirtualHost("/");//默认虚拟机virtualHost
        //创建连接
        Connection connection = factory.newConnection();
        //创建channel
        Channel channel = connection.createChannel();
        //声明队列。
        channel.queueDeclare("msgTest", true, false, false, null);
        System.out.println("====================等待消息======================");
        //接收消息
        DeliverCallback callback = (consumerTag, message) -> {
            System.out.println("接收消息:" + new String(message.getBody(), "utf-8"));
        };
        //消费
        channel.basicConsume("msgTest", true, callback, consumerTag -> {
        });
    }
}
